<?php

/**
 * phone actions.
 *
 * @package    tmdb
 * @subpackage phone
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class phoneActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->phone_list = Doctrine::getTable('Phone')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->phone = Doctrine::getTable('Phone')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->phone);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PhoneForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new PhoneForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($phone = Doctrine::getTable('Phone')->find(array($request->getParameter('id'))), sprintf('Object phone does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new PhoneForm($phone);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($phone = Doctrine::getTable('Phone')->find(array($request->getParameter('id'))), sprintf('Object phone does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new PhoneForm($phone);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($phone = Doctrine::getTable('Phone')->find(array($request->getParameter('id'))), sprintf('Object phone does not exist (%s).', array($request->getParameter('id'))));
    $phone->delete();

    $this->redirect($this->getRequest()->getReferer());
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $phone = $form->save();

      $this->redirect('phone/edit?id='.$phone['id']);
    }
  }
}
